package calorie.com.calorie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import calorie.com.calorie.R;
import calorie.com.calorie.activity.MallBuyGoodsActivity;
import calorie.com.calorie.activity.MallFinishActivity;
import calorie.com.calorie.activity.MallShippedActivity;
import calorie.com.calorie.activity.MallWaitSendActivity;
import calorie.com.calorie.activity.MallWaitSureActivity;
import calorie.com.calorie.adapter.GoodsListAdapter;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.GoodsListContact;
import calorie.com.calorie.response.GoodsListResponce;
import retrofit2.Response;


/**
 * 商城
 */
public class MallFragment extends MvpFragment<GoodsListContact.GoodsListPresent> implements GoodsListContact.IGoodsListView {


    @BindView(R.id.rb_wait_send)
    RadioButton rbWaitSend;
    @BindView(R.id.rb_over_send)
    RadioButton rbOverSend;
    @BindView(R.id.rb_wait_sure)
    RadioButton rbWaitSure;
    @BindView(R.id.rb_finish)
    RadioButton rbFinish;
    @BindView(R.id.goods_list)
    RecyclerView goodsList;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;

    List<GoodsListResponce.DataBean.GoodsListBean> mList;
    GoodsListAdapter mAdapter;

    Unbinder unbinder;

    int pageNo = 1;

    @Override
    public void initData(Bundle savedInstanceState) {
        mList = new ArrayList<>();
        mAdapter = new GoodsListAdapter(R.layout.fragment_mall_item, mList);
        GridLayoutManager manager = new GridLayoutManager(context, 2);
        goodsList.setLayoutManager(manager);
        goodsList.setAdapter(mAdapter);
        mPresent.goodsList(context, pageNo);
        mAdapter.setOnItemChildClickListener(new GoodsListAdapter.OnItemChildClickListener() {


            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                Intent intent = new Intent(context, MallBuyGoodsActivity.class);
                intent.putExtra("goods", com.alibaba.fastjson.JSONObject.toJSONString( mList.get(position)));//传入商品编号


                if (mList.get(position).getGoodsAttribute().size()!=0){
                    Gson gson = new Gson();
                    String jsonStr = gson.toJson(mList.get(position).getGoodsAttribute());
                    intent.putExtra("goodsAttribute",jsonStr);
                    Log.e("json",""+jsonStr);
                }
                intent.putExtra("imgUrl", mList.get(position).getImgUrl());//传入商品图片
                if(mList.get(position).getDetailUrl().size()!=0){
                    Gson gson = new Gson();
                    String jsonStr = gson.toJson(mList.get(position).getDetailUrl());
                    intent.putExtra("detailUrl",jsonStr);//传入商品详情图片列表
                }
                startActivity(intent);

//                mList.get(position).getGoodsAttribute();
//
//                JSONArray jsonArray = new JSONArray();
//                JSONObject jsonObject = new JSONObject();
//                JSONObject tmpObj = null;
//                int count = mList.get(position).getGoodsAttribute().size();
//                for(int i = 0; i < count; i++)
//                {
//                    tmpObj = new JSONObject();
//                    try {
//                        tmpObj.put("attribute" , mList.get(position).getGoodsAttribute().get(i).getAttribute());
//                        tmpObj.put("stock", mList.get(position).getGoodsAttribute().get(i).getStock());
//                    }catch (JSONException e){
//                        e.printStackTrace();
//                    }
//                    jsonArray.put(tmpObj);
//                }
//                String goodsAttribute = jsonArray.toString(); //
//                intent.putExtra("goodsAttribute",goodsAttribute);
//






//
//                try {
//
//                    jsonObject.put("goodsAttribute" , goodsAttribute);   // 获得JSONObject的String
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }



//                if (mList.get(position).getGoodsAttribute().size() != 0) {
//                    for (int i = 0; i < mList.get(position).getGoodsAttribute().size(); i++) {
//                        intent.putExtra("attribute", mList.get(position).getGoodsAttribute().get(i).getAttribute());//商品属性
//                        intent.putExtra("stock", mList.get(position).getGoodsAttribute().get(i).getStock());//商品库存
//                    }
//                    return;
//                }

//                intent.putStringArrayListExtra("goodsAttribute",mList.get(position).getGoodsAttribute().size())

//                intent.putExtra("attributeTwo", mList.get(position).getGoodsAttribute().get(1).getAttribute());//商品属性
//                intent.putExtra("attributeThree", mList.get(position).getGoodsAttribute().get(2).getAttribute());//商品属性
//                intent.putExtra("attributeFour", mList.get(position).getGoodsAttribute().get(3).getAttribute());//商品属性
//
//
//                intent.putExtra("stockTwo", mList.get(position).getGoodsAttribute().get(1).getStock());//商品库存
//                intent.putExtra("stockThree", mList.get(position).getGoodsAttribute().get(2).getStock());//商品库存
//                intent.putExtra("stockFour", mList.get(position).getGoodsAttribute().get(3).getStock());//商品库存


            }
        });

        refresh.setOnRefreshListener(refreshLayout -> {
            pageNo = 1;
            mPresent.goodsList(context, pageNo);
        });
        refresh.setOnLoadMoreListener(refreshLayout -> {
            pageNo++;
            mPresent.goodsList(context, pageNo);
        });

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mall;
    }

    @Override
    protected GoodsListContact.GoodsListPresent createPresent() {
        return new GoodsListContact.GoodsListPresent();
    }

    @Override
    public void getGoodsListResult(Response<GoodsListResponce> response) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            List<GoodsListResponce.DataBean.GoodsListBean> data = response.body().getData().getGoodsList();
            if (!ObjectUtils.isEmpty(data)) {
                mList.clear();
                mList.addAll(data);
                mAdapter.notifyDataSetChanged();
            }
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @OnClick({R.id.rb_wait_send, R.id.rb_over_send, R.id.rb_wait_sure, R.id.rb_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_wait_send://待发货
                Intent waitSend = new Intent(context, MallWaitSendActivity.class);
                startActivity(waitSend);
                break;
            case R.id.rb_over_send://已发货
                Intent overSend = new Intent(context, MallShippedActivity.class);
                startActivity(overSend);
                break;
            case R.id.rb_wait_sure://待确认
                Intent waitSure = new Intent(context, MallWaitSureActivity.class);
                startActivity(waitSure);
                break;
            case R.id.rb_finish://已完成
                Intent finish = new Intent(context, MallFinishActivity.class);
                startActivity(finish);
                break;
        }
    }

}
